home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13936 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  63 lines

  1. Path: kbad.eglin.af.mil!rpi!not-for-mail
  2. From: liuh2@vccsouth02.its.rpi.edu (nemish)
  3. Newsgroups: comp.lang.c++
  4. Subject: How does this look()()()()()?
  5. Date: 27 Mar 1996 15:11:09 -0500
  6. Organization: Rensselaer Polytechnic Institute
  7. Message-ID: <4jc7ct$4od@vccsouth02.its.rpi.edu>
  8. NNTP-Posting-Host: vccsouth02.its.rpi.edu
  9.  
  10. Hi,
  11.  
  12. We use to have a convient way to assign values to an
  13. array in good ol' C like this.
  14.  
  15. int vec[10] = {1,2,3,4,5,6,7,8,9,10};
  16.  
  17. Now, in C++ this kind of style is not encouraged(BTW, is this legal?)
  18. So we have to do something like.
  19.     vec[0] = 1;
  20.     vec[1] = 2;
  21.     vec[2] = 3;
  22.        .
  23.        .
  24.        .
  25.     vec[9] = 10;
  26.  
  27. which is a lot of typing.
  28.  
  29. I just had an idea that we can do something like this,
  30.  
  31.     vec(1)(2)(3)(4)(5)(6)(7)(8)(9)(10);
  32.  
  33. I wrote a simple program here with STL
  34.  
  35. #include <vector.h>
  36. #include <algo.h>
  37.  
  38. class my_vector:public vector<int> {
  39. public:
  40.   my_vector():vector<int>() {}
  41.  
  42.   my_vector& operator () (int element) {push_back(element); return (*this);}
  43. };
  44.  
  45. int main()
  46. {
  47.   my_vector vec;
  48.   ostream_iterator<int> out(cout, " ");
  49.  
  50.   vec(1)(2)(3)(4)(5)(6)(7)(8)(9)(10);
  51.   copy(vec.begin(),vec.end(),out);
  52.  
  53.   return 0;
  54. }
  55.  
  56. =========================================
  57.  
  58. What do you guys think?  Is there anything that is not appropriate, or
  59. I am doing something unnecessary or very wrong?
  60.  
  61. Hsi-Wen Liu
  62. liuh2@rpi.edu
  63.